#!/usr/bin/env php
<?php declare(strict_types=1);

use Wlsh\DI;

const SWOOLE_LOG_LEVEL = 2;
const APP_DEBUG = TRUE;
//使用error_reporting来定义哪些级别错误可以触发 -1
error_reporting(E_ALL);

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('log_errors', '1');
//每个PHP进程所吃掉的最大内存
ini_set('memory_limit', '2048M');

date_default_timezone_set('Asia/Shanghai');

const DS = DIRECTORY_SEPARATOR;
const ROOT_PATH = __DIR__;
const WLSH_CORE_PATH = __DIR__;
const CONF_PATH = ROOT_PATH . DS . 'config';
const LOG_PATH = ROOT_PATH . DS . 'log';

Swoole\Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]);
Swoole\Coroutine\run(static function () use ($argv) {
    try {
        require_once ROOT_PATH . '/vendor/autoload.php';
        require_once CONF_PATH . DS . 'environ.php';

        DI::factory()->set('config_arr', array_merge(
            require CONF_PATH . DS . 'common.php',
            require CONF_PATH . DS . CURRENT_ENV . '.php'
        ));

        if (empty($argv[1])) {
            throw new RuntimeException('路由参数错误');
        }

        $request_uri_arr = explode('/', $argv[1]);

        $class = ucfirst($request_uri_arr[0]);
        $class = "\Console\Controllers\\{$class}Controller";
        $action = "{$request_uri_arr[1]}Action";

        unset($argv[0], $argv[1]);

        $res = (new $class)->$action(...$argv);
        print_r($res);
    } catch (Throwable $e) {
        print_r($e->getMessage());
    }
});
Swoole\Event::wait();
